/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.yuhanginfo.modules.sys.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.yuhanginfo.common.config.Global;
import com.yuhanginfo.common.persistence.Page;
import com.yuhanginfo.common.web.BaseController;
import com.yuhanginfo.common.utils.StringUtils;
import com.yuhanginfo.modules.sys.entity.Area;
import com.yuhanginfo.modules.sys.entity.FreeWaybill;
import com.yuhanginfo.modules.sys.entity.User;
import com.yuhanginfo.modules.sys.service.AreaService;
import com.yuhanginfo.modules.sys.service.FreeWaybillService;
import com.yuhanginfo.modules.sys.utils.UserUtils;

/**
 * 包邮设置Controller
 * @author yangs
 * @version 2017-10-09
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/freeWaybill")
public class FreeWaybillController extends BaseController {

	@Autowired
	private FreeWaybillService freeWaybillService;
	@Autowired
	private AreaService areaService;
	
	@ModelAttribute
	public FreeWaybill get(@RequestParam(required=false) String id) {
		FreeWaybill entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = freeWaybillService.get(id);
		}
		if (entity == null){
			entity = new FreeWaybill();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:freeWaybill:view")
	@RequestMapping(value = {"list", ""})
	public String list(FreeWaybill freeWaybill, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<Area> area=areaService.useStoreList();
		List<Area> areas=areaService.selectProvinceList();
		Page<FreeWaybill> page = freeWaybillService.findPage(new Page<FreeWaybill>(request, response), freeWaybill); 
		model.addAttribute("page", page);
		model.addAttribute("area", area);
		model.addAttribute("areas", areas);
		return "modules/sys/waybillFree/freeWaybillList";
	}

	@RequiresPermissions("sys:freeWaybill:view")
	@RequestMapping(value = "form")
	public String form(FreeWaybill freeWaybill, Model model) {
		List<Area> area=areaService.useStoreList();
		List<Area> areas=areaService.selectProvinceList();
		model.addAttribute("area", area);
		model.addAttribute("areas", areas);
		model.addAttribute("freeWaybill", freeWaybill);
		return "modules/sys/waybillFree/freeWaybillForm";
	}

	@RequiresPermissions("sys:freeWaybill:edit")
	@RequestMapping(value = "save")
	public String save(FreeWaybill freeWaybill, Model model, RedirectAttributes redirectAttributes) {
		freeWaybill.setCreateDate(new Date());
		freeWaybill.setUpdateDate(new Date());
		User us = UserUtils.getUser();
		freeWaybill.setCreateBy(us);
		freeWaybill.setUpdateBy(us);
		if (!beanValidator(model, freeWaybill)){
			return form(freeWaybill, model);
		}
		freeWaybillService.save(freeWaybill);
		addMessage(redirectAttributes, "保存包邮设置成功");
		return "redirect:"+Global.getAdminPath()+"/sys/freeWaybill/?repage";
	}
	
	@RequiresPermissions("sys:freeWaybill:edit")
	@RequestMapping(value = "delete")
	public String delete(FreeWaybill freeWaybill, RedirectAttributes redirectAttributes) {
		freeWaybillService.delete(freeWaybill);
		addMessage(redirectAttributes, "删除包邮设置成功");
		return "redirect:"+Global.getAdminPath()+"/sys/freeWaybill/?repage";
	}
	
    @ResponseBody
	@RequiresPermissions("sys:freeWaybill:view")
	@RequestMapping(value = "yzAddress")
	public Map<String,Object> yzAddress(FreeWaybill freeWaybill,String receiveAreaId,String sendAreaId,String id) {
		   Map<String,Object>map=new HashMap<>();
		   freeWaybill.setReceiveAreaId(receiveAreaId);
		   freeWaybill.setSendAreaId(sendAreaId);
		   List<FreeWaybill> list=freeWaybillService.selectAddress(freeWaybill);
		   if(id!=""&&id!=null&&id.length()>0){
				if(list.size()>1 && list.get(0).getId().equals(id)){
					map.put("st", 1);
				}
				if(!list.isEmpty() && !list.get(0).getId().equals(id)){
					map.put("st", 1);
				}
			}else{
				if(!list.isEmpty()){
					map.put("st", 1);
				}
			}
		return map;
	}
	
	
}